using UnityEngine;

/// <summary>
/// 飞行组件 - 用于飞行单位（如飞行僵尸、飞行植物等）
/// </summary>
[DisallowMultipleComponent]
public class FlyingComponent : MonoBehaviour
{
    [Header("飞行配置")]
    [SerializeField] private float m_FlightHeight = 2.0f; // 飞行高度
    [SerializeField] private float m_FlightSpeed = 1.5f; // 飞行速度倍数
    [SerializeField] private float m_HoverSpeed = 0.5f; // 悬停时的上下浮动速度
    [SerializeField] private float m_HoverAmplitude = 0.3f; // 悬停幅度
    
    [Header("飞行状态")]
    [SerializeField] private bool m_IsFlying = false; // 是否正在飞行
    [SerializeField] private bool m_CanFly = true; // 是否可以飞行
    
    [Header("视觉效果")]
    [SerializeField] private GameObject m_WingsEffect; // 翅膀特效
    [SerializeField] private ParticleSystem m_FlightTrail; // 飞行轨迹特效
    
    private Vector3 m_OriginalPosition;
    private Vector3 m_FlightPosition;
    private float m_HoverTime = 0f;
    private bool m_IsInitialized = false;
    
    /// <summary>
    /// 是否正在飞行
    /// </summary>
    public bool IsFlying => m_IsFlying;
    
    /// <summary>
    /// 是否可以飞行
    /// </summary>
    public bool CanFly 
    { 
        get => m_CanFly; 
        set => m_CanFly = value; 
    }
    
    /// <summary>
    /// 飞行高度
    /// </summary>
    public float FlightHeight 
    { 
        get => m_FlightHeight; 
        set => m_FlightHeight = Mathf.Max(0f, value); 
    }
    
    /// <summary>
    /// 飞行速度倍数
    /// </summary>
    public float FlightSpeed 
    { 
        get => m_FlightSpeed; 
        set => m_FlightSpeed = Mathf.Max(0.1f, value); 
    }
    
    private void Awake()
    {
        m_OriginalPosition = transform.position;
        m_FlightPosition = m_OriginalPosition + Vector3.up * m_FlightHeight;
    }
    
    /// <summary>
    /// 初始化飞行组件
    /// </summary>
    public void Initialize()
    {
        m_IsInitialized = true;
        m_OriginalPosition = transform.position;
        m_FlightPosition = m_OriginalPosition + Vector3.up * m_FlightHeight;
        
        // 初始化特效
        InitializeEffects();
        
        Logger.Log($"{name} 飞行组件初始化 - 高度: {m_FlightHeight}, 速度倍数: {m_FlightSpeed}");
    }
    
    /// <summary>
    /// 开始飞行
    /// </summary>
    public void StartFlying()
    {
        if (!m_CanFly || m_IsFlying) return;
        
        m_IsFlying = true;
        m_HoverTime = 0f;
        
        // 激活飞行特效
        SetFlightEffects(true);
        
        Logger.Log($"{name} 开始飞行");
    }
    
    /// <summary>
    /// 停止飞行
    /// </summary>
    public void StopFlying()
    {
        if (!m_IsFlying) return;
        
        m_IsFlying = false;
        
        // 停用飞行特效
        SetFlightEffects(false);
        
        Logger.Log($"{name} 停止飞行");
    }
    
    /// <summary>
    /// 切换飞行状态
    /// </summary>
    public void ToggleFlying()
    {
        if (m_IsFlying)
        {
            StopFlying();
        }
        else
        {
            StartFlying();
        }
    }
    
    /// <summary>
    /// 设置飞行高度
    /// </summary>
    public void SetFlightHeight(float height)
    {
        m_FlightHeight = Mathf.Max(0f, height);
        m_FlightPosition = m_OriginalPosition + Vector3.up * m_FlightHeight;
    }
    
    /// <summary>
    /// 设置飞行速度倍数
    /// </summary>
    public void SetFlightSpeed(float speedMultiplier)
    {
        m_FlightSpeed = Mathf.Max(0.1f, speedMultiplier);
    }
    
    /// <summary>
    /// 获取当前移动速度倍数（飞行时使用飞行速度）
    /// </summary>
    public float GetMovementSpeedMultiplier()
    {
        return m_IsFlying ? m_FlightSpeed : 1f;
    }
    
    private void Update()
    {
        if (!m_IsInitialized) return;
        
        if (m_IsFlying)
        {
            UpdateFlyingPosition();
        }
    }
    
    /// <summary>
    /// 更新飞行位置
    /// </summary>
    private void UpdateFlyingPosition()
    {
        // 计算悬停效果
        m_HoverTime += Time.deltaTime * m_HoverSpeed;
        float hoverOffset = Mathf.Sin(m_HoverTime) * m_HoverAmplitude;
        
        // 更新飞行位置
        Vector3 targetPosition = m_FlightPosition + Vector3.up * hoverOffset;
        
        // 平滑移动到目标位置
        transform.position = Vector3.Lerp(transform.position, targetPosition, Time.deltaTime * 2f);
    }
    
    /// <summary>
    /// 初始化飞行特效
    /// </summary>
    private void InitializeEffects()
    {
        // 初始化翅膀特效
        if (m_WingsEffect != null)
        {
            m_WingsEffect.SetActive(false);
        }
        
        // 初始化飞行轨迹特效
        if (m_FlightTrail != null)
        {
            var emission = m_FlightTrail.emission;
            emission.enabled = false;
        }
    }
    
    /// <summary>
    /// 设置飞行特效
    /// </summary>
    private void SetFlightEffects(bool active)
    {
        // 翅膀特效
        if (m_WingsEffect != null)
        {
            m_WingsEffect.SetActive(active);
        }
        
        // 飞行轨迹特效
        if (m_FlightTrail != null)
        {
            var emission = m_FlightTrail.emission;
            emission.enabled = active;
        }
    }
    
    /// <summary>
    /// 强制降落（用于被击落等情况）
    /// </summary>
    public void ForceLanding()
    {
        if (!m_IsFlying) return;
        
        StopFlying();
        
        // 立即移动到地面
        transform.position = m_OriginalPosition;
        
        Logger.Log($"{name} 被强制降落");
    }
    
    /// <summary>
    /// 检查是否在空中
    /// </summary>
    public bool IsInAir()
    {
        return transform.position.y > m_OriginalPosition.y + 0.1f;
    }
    
    /// <summary>
    /// 获取当前飞行高度
    /// </summary>
    public float GetCurrentFlightHeight()
    {
        return transform.position.y - m_OriginalPosition.y;
    }
    
    private void OnDestroy()
    {
        StopFlying();
    }
    
    private void OnDisable()
    {
        StopFlying();
    }
    
    #if UNITY_EDITOR
    /// <summary>
    /// 在编辑器中显示飞行范围
    /// </summary>
    private void OnDrawGizmosSelected()
    {
        // 绘制飞行高度
        Gizmos.color = Color.cyan;
        Vector3 flightPos = transform.position + Vector3.up * m_FlightHeight;
        Gizmos.DrawLine(transform.position, flightPos);
        Gizmos.DrawWireSphere(flightPos, 0.5f);
        
        // 绘制悬停范围
        Gizmos.color = Color.yellow;
        Gizmos.DrawWireCube(flightPos, new Vector3(2f, m_HoverAmplitude * 2, 2f));
    }
    #endif
}
